package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-21
 */
@Api(description = "课程里面的章节")
@RestController
@RequestMapping("/eduservice/chapter")
//原来需要在Controller上面加上@CrossOrigin解决跨域问题，现在gateway已经处理了   /*解决跨域问题*/
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /*课程大纲列表*/
    /*根据课程id查询课程里面的章节小节*/
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list =chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "添加课程里面的章节信息")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }

    /*根据章节id进行查询*/
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("eduChapter",eduChapter);
    }

    /*修改章节*/
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        boolean flag = chapterService.updateById(eduChapter);
        if(flag==true){
            return R.ok();
        }
        return R.error();
    }


    /*根据章节id进行删除*/
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}












